package com.yycx.module.user.provider.handler;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.enums.ColleconEnum;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.system.client.entity.SysCollecon;
import com.yycx.module.user.client.entity.AppUser;
import org.springframework.stereotype.Component;

/**
 * 查询用户被关注用户列表(粉丝列表)
 *
 * @author zyf
 */
@Component
public class SelectUserListByFsHandler implements PageInterceptor<AppUser> {
    @Override
    public void prepare(CriteriaQuery<AppUser> cq, PageParams pageParams, EntityMap params) {
        Long otherUserId = params.getLong("userId");
        Long userId = OpenHelper.getUserId();
        cq.clear();
        cq.select(AppUser.class, "userId", "nickName", "avatar","userType", "userNo", "shareCode", "tags", "motto");
        //关注标记
        cq.addSelect("collecon_tag(3,'AppUser',collecon.userId," + userId + ") gzTag");
        cq.eq(SysCollecon.class, "optType", ColleconEnum.GZ.getCode());
        cq.eq(SysCollecon.class, "targetEntity", "AppUser");
        if (FlymeUtils.isNotEmpty(otherUserId)) {
            cq.eq(SysCollecon.class, "targetId", otherUserId);
        } else {
            cq.eq(SysCollecon.class, "targetId", userId);
        }
        String keyword = params.get("keyword");
        if (FlymeUtils.isNotEmpty(keyword)) {
            cq.and(e->e.like(true,"tags",keyword).or().like(true,"nickName",keyword));
        }
        cq.orderByDesc("collecon.createTime");
        cq.createJoin(SysCollecon.class).setJoinField("userId").setMainField("userId");

    }

}
